package it.polimi.traveldream.web.beans.login;

import java.util.List;

import it.polimi.traveldream.managers.credentials.UserCredentialManager;
import it.polimi.traveldream.persistence.dtos.UserDTO;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name = "userBean")
@ViewScoped
public class UserBean {

	@EJB
	private UserCredentialManager userMgr;

	private String selectedUser;

	private UserDTO self;

	private boolean add;

	private String nome;

	private String cognome;

	private String email;

	public UserBean() {
		self = new UserDTO();
		setAdd(false);
	}

	public void istanzia() {
		self = new UserDTO();
		setNome(getName());
		setCognome(getSurname());
		setEmail(getMail());
		self.setFirstName(getName());
		self.setEmail(getMail());
		self.setLastName(getSurname());
		self.setIsClient(userMgr.getUserDTO().getIsClient());
		self.setPassword(userMgr.getUserDTOP().getPassword());
		selectedUser = new String();

	}

	public void aggiorna() {

	}

	public String getName() {
		return userMgr.getUserDTO().getFirstName();
	}

	public String getSurname() {
		return userMgr.getUserDTO().getLastName();
	}

	public boolean getIsCliente() {
		return userMgr.getUserDTO().getIsClient();
	}

	public List<UserDTO> getAllUsers() {
		return userMgr.getAllUsersDTO();
	}

	public String getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(String selectedUser) {
		this.selectedUser = selectedUser;
	}

	public String getMail() {
		return userMgr.getUserDTO().getEmail();
	}

	public void update() {
		self = userMgr.getUserDTOP();
		self.setEmail(email);
		self.setFirstName(nome);
		self.setPassword(userMgr.getUserDTOP().getPassword());
		self.setLastName(cognome);

		self.setIsClient(userMgr.getUserDTOP().getIsClient());
		userMgr.updates(self);
		userMgr.updateNome(nome, email);
	}

	public UserDTO getSelf() {
		return self;
	}

	public void setSelf(UserDTO self) {
		this.self = self;
	}

	public void delUser() {
		System.out.println(selectedUser + "LUILUI");
		userMgr.remove(selectedUser);

	}

	public void delUser2(String email) {
		System.out.println(selectedUser + "LUILUI");
		userMgr.remove(email);

	}

	public boolean isAdd() {
		return add;
	}

	public void setAdd(boolean add) {
		this.add = add;
	}

	public void toggle() {
		add = !add;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public String getCognome() {
		return cognome;
	}

	public void setCognome(String cognome) {
		this.cognome = cognome;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}